﻿#region 程序集 VanillaPsycastsExpanded, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null
// C:\code\MyMod\MOD\bin\this!!!!!!!1\1.4\Assemblies\VanillaPsycastsExpanded.dll
// Decompiled with ICSharpCode.Decompiler 7.1.0.6543
#endregion

using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using RI_Talisman;
using RimWorld;
using RimWorld.QuestGen;
using RimWorld.SketchGen;
using UnityEngine;
using Verse;
using Verse.Noise;

namespace WhoXiuXian
{
    public static class RI_RandomUtility
    {
        public static List<IntVec3> CutRectToCellListBySize(CellRect rect, int width, int height)
        {
            List<IntVec3> rectList = new List<IntVec3>();
            for (int x = rect.minX; x < rect.maxX; x++)
            {
                for (int z = rect.minZ; z < rect.maxZ; z++)
                {
                    IntVec3 temp = new IntVec3(x, 0, z);
                    if (!IsOnEdge(temp, rect, width > height ? width - 1 : height - 1))
                    {
                        if ((x - rect.minX) % width == 0 && (z - rect.minZ) % height == 0)
                        {
                            rectList.Add(temp);
                        }
                    }
                }
            }
            return rectList;
        }

        public static List<IntVec3> FindWallsFromRect(ResolveParams parms, CellRect rect, ThingDef thingDef)
        {
            List<IntVec3> rectList = new List<IntVec3>();
            int xEast = rect.CenterCell.x;
            int xWest = rect.CenterCell.x;
            int zNorth = rect.CenterCell.z;
            int zSorth = rect.CenterCell.z;

            while (xEast - rect.CenterCell.x < 20)
            {
                if (parms.sketch.ThingsAt(new IntVec3(xEast, 0, rect.CenterCell.z)).Count() > 0)
                {
                    if (parms.sketch.ThingsAt(new IntVec3(xEast, 0, rect.CenterCell.z)).First().def != thingDef)
                    {
                        xEast++;
                    }
                    else
                    {
                        break;
                    }
                }
                xEast++;
            }

            while (rect.CenterCell.x - xWest < 20)
            {
                if (parms.sketch.ThingsAt(new IntVec3(xWest, 0, rect.CenterCell.z)).Count() > 0)
                {
                    if (parms.sketch.ThingsAt(new IntVec3(xWest, 0, rect.CenterCell.z)).First().def != thingDef)
                    {
                        xWest--;
                    }
                    else
                    {
                        break;
                    }
                }
                xWest--;
            }

            while (zNorth - rect.CenterCell.z < 20)
            {
                if (parms.sketch.ThingsAt(new IntVec3(rect.CenterCell.x, 0, zNorth)).Count() > 0)
                {
                    if (parms.sketch.ThingsAt(new IntVec3(rect.CenterCell.x, 0, zNorth)).First().def != thingDef)
                    {
                        zNorth++;
                    }
                    else
                    {
                        break;
                    }
                }
                zNorth++;
            }

            while (rect.CenterCell.z - zSorth < 20)
            {
                if (parms.sketch.ThingsAt(new IntVec3(rect.CenterCell.x, 0, zSorth)).Count() > 0)
                {
                    if (parms.sketch.ThingsAt(new IntVec3(rect.CenterCell.x, 0, zSorth)).First().def != thingDef)
                    {
                        zSorth--;
                    }
                    else
                    {
                        break;
                    }
                }
                zSorth--;
            }

            //while (parms.sketch.ThingsAt(new IntVec3(xWest, 0, rect.CenterCell.z)).Count() > 0)
            //{
            //    if (parms.sketch.ThingsAt(new IntVec3(xWest, 0, rect.CenterCell.z)).First().def != thingDef)
            //    {
            //        xWest--;
            //    }
            //}
            //while (parms.sketch.ThingsAt(new IntVec3(rect.CenterCell.x, 0, zNorth)).Count() > 0)
            //{
            //    if (parms.sketch.ThingsAt(new IntVec3(rect.CenterCell.x, 0, zNorth)).First().def != thingDef)
            //    {
            //        zNorth++;
            //    }
            //}
            //while (parms.sketch.ThingsAt(new IntVec3(rect.CenterCell.x, 0, zSorth)).Count() > 0)
            //{
            //    if (parms.sketch.ThingsAt(new IntVec3(rect.CenterCell.x, 0, zSorth)).First().def != thingDef)
            //    {
            //        zSorth--;
            //    }
            //}

            for (int x = xWest; x <= xEast; x++)
            {
                for (int z = zSorth; z <= zNorth; z++)
                {
                    if (x == xWest || x == xEast || z == zNorth || z == zSorth)
                    {
                        rectList.Add(new IntVec3(x, 0, z));
                        Log.Warning(x + "=然后=" + z);
                    }
                }
            }
            return rectList;
        }

        public static bool IsOnEdge(IntVec3 c, CellRect rect, int distFromEastAndNorthEdge)
        {
            if ((c.x != rect.minX || c.z < rect.minZ || c.z > rect.maxZ)
                && (c.x != rect.maxX || c.z < rect.minZ || c.z > rect.maxZ)
                && (c.z != rect.minZ || c.x < rect.minX || c.x > rect.maxX))
            {
                if (c.x < rect.maxX - distFromEastAndNorthEdge && c.z < rect.maxZ - distFromEastAndNorthEdge)
                {
                    if (c.z == rect.maxZ && c.x >= rect.minX)
                    {
                        return c.x <= rect.maxX;
                    }
                    return false;
                }
            }
            return true;
        }

        public static List<IntVec3> GetCircularRegion(IntVec3 center, int radius)
        {
            List<IntVec3> result = new List<IntVec3>();

            for (int x = center.x - radius; x <= center.x + radius; x++)
            {
                for (int z = center.z - radius; z <= center.z + radius; z++)
                {
                    int dx = Mathf.Abs(x - center.x);
                    int dz = Mathf.Abs(z - center.z);

                    if (dx + dz <= radius)
                    {
                        result.Add(new IntVec3(x, 0, z));
                    }
                }
            }

            return result;
        }

        public static void ReplaceWallAndBreakThem(CellRect rect, Sketch sketch, ThingDef wallDef, int breakChance, float intBreakRange)
        {
            var intInRange = RI_RandomUtility.GetCircularRegion(rect.CenterCell, 15);
            foreach (var intVec1 in intInRange)
            {
                if (sketch.ThingsAt(intVec1).Count() > 0)
                {
                    if (sketch.ThingsAt(intVec1).First().def == ThingDefOf.Wall)
                    {
                        if (Random.Range(0, 100) > (100 - breakChance))
                        {
                            sketch.AddThing(wallDef, intVec1, Rot4.Random, null, 1, null, Random.Range((int)(wallDef.BaseMaxHitPoints * intBreakRange), wallDef.BaseMaxHitPoints));
                        }
                        else {
                            sketch.AddThing(wallDef, intVec1, Rot4.Random);

                        }
                    }
                }
            }
        }

        public static void SendIncidentLetter(TaggedString baseLetterLabel, TaggedString baseLetterText, LetterDef baseLetterDef, IncidentParms parms, LookTargets lookTargets, IncidentDef def, params NamedArgument[] textArgs)
        {
            if (parms.silent || !parms.sendLetter)
            {
                return;
            }

            if (baseLetterLabel.NullOrEmpty() || baseLetterText.NullOrEmpty())
            {
                Log.Error("Sending standard incident letter with no label or text.");
            }

            TaggedString taggedString = baseLetterText.Formatted(textArgs);
            TaggedString text;
            if (parms.customLetterText.NullOrEmpty())
            {
                text = taggedString;
            }
            else
            {
                List<NamedArgument> list = new List<NamedArgument>();
                if (textArgs != null)
                {
                    list.AddRange(textArgs);
                }

                list.Add(taggedString.Named("BASETEXT"));
                text = parms.customLetterText.Formatted(list.ToArray());
            }

            TaggedString taggedString2 = baseLetterLabel.Formatted(textArgs);
            TaggedString label;
            if (parms.customLetterLabel.NullOrEmpty())
            {
                label = taggedString2;
            }
            else
            {
                List<NamedArgument> list2 = new List<NamedArgument>();
                if (textArgs != null)
                {
                    list2.AddRange(textArgs);
                }

                list2.Add(taggedString2.Named("BASELABEL"));
                label = parms.customLetterLabel.Formatted(list2.ToArray());
            }

            ChoiceLetter choiceLetter = LetterMaker.MakeLetter(label, text, parms.customLetterDef ?? baseLetterDef, lookTargets, parms.faction, parms.quest, parms.letterHyperlinkThingDefs);
            List<HediffDef> list3 = new List<HediffDef>();
            if (!parms.letterHyperlinkHediffDefs.NullOrEmpty())
            {
                list3.AddRange(parms.letterHyperlinkHediffDefs);
            }

            if (def != null && !def.letterHyperlinkHediffDefs.NullOrEmpty())
            {
                list3.AddRange(def.letterHyperlinkHediffDefs);
            }

            choiceLetter.hyperlinkHediffDefs = list3;
            Find.LetterStack.ReceiveLetter(choiceLetter);
        }
    }
}


